package com.limanyik.family.common.utils;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ShiroMD5Util {
    /**
     * 加密方式，和配置文件保持一致
     */
    @Value("${shiro.hash-algorithm-name}")
    private String HASH_ALGORITHM_NAME;
    /**
     * 加密次数
     */
    @Value("${shiro.hash-iterations}")
    private Integer HASH_ITERATIONS;

    private static ShiroMD5Util shiroMD5Util;

    @PostConstruct
    public void init() {
        shiroMD5Util = this;
    }

    /**
     * 加密MD5
     *
     * @param val
     * @return
     */
    public static String encryption(String val, String salt) {
        return new SimpleHash(shiroMD5Util.HASH_ALGORITHM_NAME, val, ByteSource.Util.bytes(salt), shiroMD5Util.HASH_ITERATIONS).toString().toUpperCase();
    }

}
